package com.bluedot.www.core.service;

import com.bluedot.www.core.dao.Execute;
import com.bluedot.www.core.pojo.DO.Log;
import com.bluedot.www.core.pojo.DTO.SysInfo;
import com.bluedot.www.core.utils.ExcelUtils;
import com.bluedot.www.core.utils.SysInfoUtil;
import com.bluedot.www.framework.mvc.annotation.MyService;
import com.bluedot.www.framework.mvc.servlet.http.ResponseEntity;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 日志表 服务实现类
 * </p>
 *
 * @author DengHongWei
 * @since 2021-08-07
 */
@MyService
public class LogService {


    private static final String LOG_MAPPER_ADDRESS = "com.bluedot.www.core.mapper.xml.LogMapper.";

    public static Object execute(String methodName, Object[] param) {
        LogService logService = new LogService();
        Object result = null;
        switch (methodName) {
            case "downLoadSomeLogFileById":
                result = logService.downLoadSomeLogFileById((List<Long>) param[0]);
                break;
            case "deleteSomeLogFileById":
                result = logService.deleteSomeLogFileById((List<Long>) param[0]);
                break;
            case "getSysInfo":
                result = logService.getSysInfo();
                break;
            default:
                throw new RuntimeException("Method NOT FOUND, method name: " + methodName);
        }
        return result;
    }

    /**
     * 根据实体类，添加、插入一条日志信息
     *
     * @param log 日志实体类
     * @return boolean
     * @author DengHongWei
     * @date 2021/8/8 15:50
     */
    private boolean insertLog(Log log) {
        Integer execute = (Integer) Execute.execute(Execute.UPDATE_TYPE, LOG_MAPPER_ADDRESS + "insertLog", new Log[]{log});
        return execute > 0;
    }

    /**
     * 根据日志id，下载多条日志信息
     *
     * @param ids 日志id的list集合
     * @return boolean
     * @author DengHongWei
     * @date 2021/8/30 12:35
     */
    private ResponseEntity downLoadSomeLogFileById(List<Long> ids) {
        ArrayList<Log> logs = new ArrayList<>();
        for (Long id : ids) {
            List<Log> log = (List<Log>) Execute.execute(Execute.QUERY_TYPE, LOG_MAPPER_ADDRESS + "findLogById", new Long[]{id});
            //输入为表格导出
            if (log.size() == 0){
                return null;
            }
            logs.add(log.get(0));
        }
        try {
            return new ResponseEntity("日志信息.xlsx", ExcelUtils.logToExcel(logs, new ByteArrayOutputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据日志id，删除多条日志信息
     *
     * @param ids 日志id的list集合
     * @return boolean
     * @author DengHongWei
     * @date 2021/8/8 15:50
     */
    private boolean deleteSomeLogFileById(List<Long> ids) {
        for (Long id : ids) {
            Integer execute = (Integer) Execute.execute(Execute.UPDATE_TYPE, LOG_MAPPER_ADDRESS + "deleteLogById", new Long[]{id});
            if (execute == 0) {
                //事务回滚，后面添加
                return false;
            }
        }
        return true;
    }


    /**
     * 查询所有的系统监控信息并返回
     *
     * @return SysInfo
     * @author DengHongWei
     * @date 2021/8/30 12:09
     */
    private SysInfo getSysInfo() {
        return SysInfoUtil.getInfo();
    }


    /**
     * 定时删除
     *
     * @param time 定时删除几天以前的数据
     * @return Integer
     * @author Ding YuXing
     * @date 2021/9/7 16:41
     */
    private Integer timerDeletion(Integer time) {
        Integer execute = (Integer) Execute.execute(Execute.UPDATE_TYPE, LOG_MAPPER_ADDRESS + "timerDeletion", new Object[]{time});
        return execute;
    }

}
